<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico">
  <link rel="mask-icon" href="/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.dqv5.com","root":"/","images":"/images","scheme":"Mist","darkmode":true,"version":"8.21.1","exturl":false,"sidebar":{"position":"right","width_expanded":320,"width_dual_column":240,"display":"always","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":null},"fold":{"enable":true,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Minesweeper              #mainDiv{             width:200px;             height: 310px;             margin: 0 auto;             border: 10px solid #C0C0C0;         }         #statusBar{">
<meta property="og:type" content="website">
<meta property="og:title" content="dqv5.com">
<meta property="og:url" content="http://blog.dqv5.com/pages/Minesweeper/minesweeper.html">
<meta property="og:site_name" content="dqv5.com">
<meta property="og:description" content="Minesweeper              #mainDiv{             width:200px;             height: 310px;             margin: 0 auto;             border: 10px solid #C0C0C0;         }         #statusBar{">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-11-23T11:58:37.875Z">
<meta property="article:modified_time" content="2024-11-23T11:58:37.875Z">
<meta property="article:author" content="du qian">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://blog.dqv5.com/pages/Minesweeper/minesweeper">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":false,"lang":"zh-CN","comments":true,"permalink":"http://blog.dqv5.com/pages/Minesweeper/minesweeper.html","path":"pages/Minesweeper/minesweeper.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title> | dqv5.com
</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?1752ae624eedea5ce859335ad2f0e657"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">dqv5.com</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">49</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">20</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a></li><li class="menu-item menu-item-留言板"><a href="/message-board/" rel="section"><i class="fa fa-comments fa-fw"></i>留言板</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="du qian"
      src="/images/soccer.jpg">
  <p class="site-author-name" itemprop="name">du qian</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">20</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">49</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/duqian42707" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;duqian42707" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/duqian42707" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;duqian42707" rel="noopener me" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/duqian42707" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;duqian42707" rel="noopener me" target="_blank"><i class="fas fa-link fa-fw"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://duqian42707.github.io/qiniu-host" title="图床 → https:&#x2F;&#x2F;duqian42707.github.io&#x2F;qiniu-host" rel="noopener me" target="_blank"><i class="far fa-image fa-fw"></i>图床</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner page posts-expand">


    
    
    
    <div class="post-block" lang="zh-CN"><header class="post-header">

<h1 class="post-title" itemprop="name headline">
</h1>

<div class="post-meta-container">
  <ul class="breadcrumb">
            <li><a href="/pages/">PAGES</a></li>
            <li><a href="/pages/Minesweeper/">MINESWEEPER</a></li>
            <li>MINESWEEPER</li>
  </ul>
</div>

</header>

      
      
      
      <div class="post-body">
          <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Minesweeper</title>
    <style>
        #mainDiv{
            width:200px;
            height: 310px;
            margin: 0 auto;
            border: 10px solid #C0C0C0;
        }
        #statusBar{
            height: 100px;
            border-bottom: 10px solid #c0c0c0;
        }
        #timer{float: left}
        #btn{
            width: 80px;
            height: 80px;
            margin: 0 auto;
            background-color: yellow;
        }
        #mines{float: right}
    </style>
</head>
<body>
<div id="mainDiv">

    <div id="statusBar">
        <div id="timer"><span id="timer_span">000</span></div>
        <div id="mines"><span>009</span></div>
        <div id="btn" onclick="start()">开始</div>
    </div>
    <canvas id="myCanvas" width="200" height="200"></canvas>

</div>
<script>

    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");

    var time;
    var int;

    var a=20
    var mineNum=9;
    var maxRow =10;
    var maxCol=10;
    var blocks=[]
    var block={value:0,state:'0',row:0,col:0}
    var minesIndex=[]
    var markFlag=true;

    //游戏开始
    var start=function(){
        //计时器启动
        time = 0;
        clearInterval(int);
        int=setInterval(function(){
            time+=1;
            document.getElementById("timer_span").innerHTML=time;
        },1000);
        //以下开始初始化
        //生成所有的雷
        for(var i=0;i<mineNum;i++){
            randomMine()
        }
        //生成所有的块，没有雷的块value=0
        for(var i=0 ;i< maxRow;i++){
            for(var j=0 ;j< maxCol;j++){
                var value=0;
                if(minesIndex.indexOf(i*maxCol+j)!=-1){
                    value=-1
                }
                var block={value:value,state:'0',row:i,col:j}
                blocks.push(block);
            }
        }
        calculate()
        //画出所有的雷
        for(var i=0;i<blocks.length;i++){
            drawBlock(blocks[i].row,blocks[i].col,"0")
        }
    }
    //随机生成一个雷
    var randomMine = function(){
        var index = parseInt(Math.random()*(maxRow*maxCol))
        if(minesIndex.indexOf(index)==-1){
            minesIndex.push(index)
        }else{
            randomMine()
        }
    }

    //计算没有雷的块周围雷的个数（value值）
    var calculate=function(){
        for(var i=0 ;i< maxRow;i++) {
            for (var j = 0; j < maxCol; j++) {
                var num=0;
                var block=blocks[i*maxCol+j]
                var row=block.row
                var col=block.col
                var row3=[row-1,row,row+1]
                var col3=[col-1,col,col+1]
                if(block.value==-1)continue;
                for(var x=0;x<row3.length;x++){
                    if(row3[x]<0||row3[x]>=maxRow)continue;
                    for(var y=0;y<col3.length;y++){
                        if(col3[y]<0||col3[y]>=maxCol)continue;
                        if(row3[x]==row&&col3[y]==col)continue;
                        var block2=blocks[row3[x]*maxCol+col3[y]]
                        if(block2.value==-1)num++;
                    }
                }
                block.value=num;
            }
        }
    }
    //画
    var drawImg=function(src,x,y){
        var img = new Image()
        img.onload=function(){
            context.drawImage(img,x,y);
        };
        img.src=src;
    }
    var drawBlock = function(row,col,state){
        var block = blocks[row*maxCol+col];
        var value = block.value;
        //if(block.value==0)return;
        var x = row*a
        var y = col*a
        switch (state){
            case "2":
                drawImg('image/flag.jpg',x,y);
                break;
            case "3":
                drawImg('image/flag2.jpg',x,y);
                break;
            case "11":
                drawImg('image/bomb0.jpg',x,y);
                break;
            case "1":
                if(value==-1){
                    drawImg('image/bomb.jpg',x,y);
                }else{
                    drawImg('image/'+value+'.jpg',x,y);
                }
                break;
            default:
                drawImg('image/s.jpg',x,y);
                break;
        }
    }

    var leftClick=function(row,col){
        var block=blocks[row*maxCol+col]
        if(block.state!='0'){
            return;
        }
        if(block.value==-1){
            console.log('游戏结束')
            for(var i=0;i<blocks.length;i++){
                drawBlock(blocks[i].row,blocks[i].col,"1")
            }
            drawBlock(row,col,'11')
            return;
        }else{
            block.state='1'
            drawBlock(row,col,'1');
            if(block.value==0){
                //循环周围8个块，依次执行左键单击事件。
                var row=block.row
                var col=block.col
                var row3=[row-1,row,row+1]
                var col3=[col-1,col,col+1]
                for(var x=0;x<row3.length;x++){
                    if(row3[x]<0||row3[x]>=maxRow)continue;
                    for(var y=0;y<col3.length;y++){
                        if(col3[y]<0||col3[y]>=maxCol)continue;
                        if(row3[x]==row&&col3[y]==col)continue;
                        leftClick(row3[x],col3[y])
                    }
                }
            }
        }
        //判断游戏是否结束
        var success=true;
        for(var i=0 ;i<blocks.length;i++){
            if(blocks[i].value==-1)continue;
            if(blocks[i].state!='1'){
                success=false;
                break;
            }
        }
        if(success){
            setTimeout(function(){
                alert('您成功了！')
            },200)
        }
    }
    var rightClick=function(row,col){
        var block=blocks[row*maxCol+col]
        var state=block.state;
        if(state=='1')return;
        if(state=='0'){
            state='2'
        }else if(state=='2'){
            if(markFlag){
                state='3'
            }else{
                state='0'
            }
        }else if(state=='3'){
            state='0'
        }
        block.state=state;
        drawBlock(row,col,state)
    }

    //禁止右键弹出菜单
    document.getElementById("mainDiv").oncontextmenu=function(){
        return false
    }
    //鼠标事件
    document.getElementById("myCanvas").onmouseup=function(ev){
        //获取事件在canvas中发生的位置
        var x, y;
        if (ev.layerX || ev.layerX == 0) {
            x = ev.layerX;
            y = ev.layerY;
        } else if (ev.offsetX || ev.offsetX == 0) { // Opera
            x = ev.offsetX;
            y = ev.offsetY;
        }
        var row=parseInt(x/a)
        var col=parseInt(y/a)
        if(ev.button==0){
            leftClick(row,col)
        }else if(ev.button==2){
            rightClick(row,col)
        }
    }

</script>
</body>
</html>
      </div>
      
      
      
    </div>
  <ul class="breadcrumb">
            <li><a href="/pages/">PAGES</a></li>
            <li><a href="/pages/Minesweeper/">MINESWEEPER</a></li>
            <li>MINESWEEPER</li>
  </ul>

    
    


    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备17034993号-1 </a>
  </div>
  <div class="copyright">
    &copy; 2016 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">du qian</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>
  <div class="upyun-league">
    本网站由<a target="_blank" rel="noopener" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" style="border-bottom: none;">
      <img src="/images/upyun_logo.png" alt="又拍云" style="display: inline;height: 30px;vertical-align: bottom;"/>
    </a>提供CDN加速/云存储服务
  </div>


    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/duqian42707" class="github-corner" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":"forest","js":{"url":"/lib/mermaid/dist/mermaid.min.js","integrity":"sha256-G8ouPAnw4zzMbnAenHnVz6h9XpKbNdOkrqTh7AadyHs="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>





  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<link rel="stylesheet" href="/lib/gitalk/dist/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"duqian42707","repo":"blog-comment","client_id":"c9f48bc2fea3d4ce5d93","client_secret":"154d6227d3d7b532b36bfc1ce030121f0263ac90","admin_user":"duqian42707","distraction_free_mode":false,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","js":{"url":"/lib/gitalk/dist/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"96a6c8cbf2e0977053f80002f9ed3bb5"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>

</body>
</html>
